package com.umfwechat.util;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;

import javax.crypto.spec.DESKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;


public class DESCrypt {
		
	
	private static final String ALGORITHM = "DES";
	
	private static final String TRANSFORMATION = "DES/ECB/PKCS5Padding";

	private static final String DES_KEY = "KSagZxG3";//��Կ

	private static DESCrypt _instance = new DESCrypt();
	
	public DESCrypt() {
	}

	public static DESCrypt getInstance() {
		return _instance;
	}
	
	
	public  String encrypt(String data) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
		if("".equals(data) || data == null){
			return data;
		}
        return encrypt(data.getBytes("GBK"),DES_KEY.getBytes("GBK"));
	}
	/**
	 * 
	 * @Description: (DES����) 
	 * @param    data  ��Ҫ���ܵ�����  encKey ��Կ
	 * @return String
	 * @author myz   
	 * @date 
	 * @throws
	 */
	public  String encryptStr(String data,String encKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
		
		if(StringUtil.isEmpty(data)){
			return data;
		}
		if(StringUtil.isEmpty(encKey)){
			return encrypt(data.getBytes("GBK"),DES_KEY.getBytes("GBK"));
		}
		return encrypt(data.getBytes("GBK"),encKey.getBytes("GBK"));
	}
	
	public String encrypt(byte[] src, byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
		
		Cipher cipher = Cipher.getInstance(TRANSFORMATION);
		SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
		KeySpec keySpec = new DESKeySpec(key);
		SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
		cipher.init(Cipher.ENCRYPT_MODE, secretKey, new SecureRandom());
		byte[] enMsgBytes = cipher.doFinal(src);
		String hexString = toHexString(new String(Base64.encode(enMsgBytes)));
		return hexString;
	}
	
	
	public String decrypt(String decData) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return new String(decrypt(Base64.decode(toStringHex(decData)),DES_KEY.getBytes("GBK")));
	}
	
	/**
	 * 
	 * @Description: (DES����) 
	 * @param    decData ��Ҫ���ܵ�����  decKey ��Կ
	 * @return String
	 * @author myz   
	 * @date 
	 * @throws
	 */
	public String decryptStr(String decData,String decKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
		if(StringUtil.isEmpty(decKey)){
			return new String(decrypt(Base64.decode(toStringHex(decData)),DES_KEY.getBytes("GBK")));
		}
		return new String(decrypt(Base64.decode(toStringHex(decData)),decKey.getBytes("GBK")));
	}
	
	public byte[] decrypt(byte[] encryptBytes, byte[] key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, BadPaddingException {
		
		Cipher deCipher = Cipher.getInstance(TRANSFORMATION);
		SecretKeyFactory deDecretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
		KeySpec deKeySpec = new DESKeySpec(key);
		SecretKey deSecretKey = deDecretKeyFactory.generateSecret(deKeySpec);
		deCipher.init(Cipher.DECRYPT_MODE, deSecretKey, new SecureRandom());
		byte[] deMsgBytes = deCipher.doFinal(encryptBytes);
		return deMsgBytes;
	}
	
	/*
	 * �ַ�ת16����
	 */
	public String toHexString(String s) {
		String str = "";
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			str = str + s4;
		}
		return str;
	}
	
	/*
	 * 16����ת�ַ�
	 */
	public String toStringHex(String s) {
		byte[] baKeyword = new byte[s.length() / 2];
		for (int i = 0; i < baKeyword.length; i++) {

			try {
				baKeyword[i] = (byte) (0xff & Integer.parseInt(
						s.substring(i * 2, i * 2 + 2), 16));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		try {
			s = new String(baKeyword, "utf-8");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		return s;
	}
	
}
